/***************************************************************************
 *   Copyright (C) 2007 by Ivan Topolsky   *
 *   ivan.topolsky@genebio.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <fstream>

#include "utils/runlcmsmsreadermgf.h"

using namespace std;
using namespace bol;

#define STRINGIFY(x) XSTRINGIFY(x)
#define XSTRINGIFY(x) #x

void printNearPrecursor(const double mass);
static RunLcmsms run;
static RunLcmsms run2;

int main(int argc, char **argv){
  string file;
  
  // Verify that the version of the library that we linked against is
  // compatible with the version of the headers we compiled against.
#ifdef HAVE_PROTOBUF
  GOOGLE_PROTOBUF_VERIFY_VERSION;
#endif

  const char *tmpname = "/tmp/testser.bin";

  if(argc>1){
    file=string(argv[1]);
  }else{
    file=STRINGIFY(DATADIR) "/a.mgf";
  }

  RunLcmsmsReaderMGF reader;
  reader.read(run, file);
  for(int i=0; i<run.msmsCollection().size(); i++){
    cout<<run.msmsCollection()[i]<<endl;
  }

#ifdef HAVE_PROTOBUF
  // save data to archive
  {
    cout << "serializing" << endl;
    fstream os(tmpname, ios::out | ios::trunc | ios::binary);

    protobuf::RunLcmsms pb;

    // operator declared in the include file will take care of populating the google protobuf serializer
    pb << run;

    if (!pb.SerializeToOstream(&os)) {
      cerr << "Failed to serialize data." << endl;
      return -1;
    }
  }
  // to manually inspect output :
  //  protoc --decode=bol.protobuf.runlcmsms runlcmsms.proto < /tmp/testser.bin  | less

  // reload data from archive
  {
    cout << "re-loading" << endl;
    fstream input(tmpname, ios::in | ios::binary);

    protobuf::RunLcmsms pb;

    if (!pb.ParseFromIstream(&input)) {
      cerr << "Failed to reload data." << endl;
      return -1;
    }

    // operator declared in the include file will take care of populating the RunLcms object
    pb >> run2;
  }

  for(int i=0; i<run2.msmsCollection().size(); i++){
    cout<<run2.msmsCollection()[i]<<endl;
  }
#else
    cerr << "google protocole buffer serialisation isn't compiled in" << endl;
#endif

  return 0;
}
